// BOM清单

<template>
  <view>
    <mes-search
      placeholder="单据编号"
      v-model="postForm.purchase_code"
      @search="resetPaging(), getData()"
    ></mes-search>
    <mes-card-index :form="cardIndexForm">
      <template v-slot:subhead="{ data }">{{ data.row.name || '--' }}</template>
    </mes-card-index>
    <mes-tip v-if="noData"></mes-tip>
    <mes-button type="1" @tap="add"></mes-button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      cardIndexForm: {
        items: [],
        actionSheet: [
          {
            name: '详情',
            event: (row) => {
              this.detail(row)
            }
          },
          {
            name: '编辑',
            event: (row) => {
              this.edit(row)
            }
          },
          {
            name: '删除',
            showModal: true,
            event: (row) => {
              this.del(row)
            }
          }
        ]
      },
      noData: false,
      hasMore: true,
      postForm: {
        purchase_code: '',
        page: 1
      }
    }
  },
  onShow() {
    getApp().globalData.goods = null
    this.resetPaging()
    this.getData()
  },
  onReachBottom() {
    if (!this.hasMore) return
    this.postForm.page++
    this.getData()
  },
  methods: {
    // 重置分页
    resetPaging() {
      this.hasMore = true
      this.postForm.page = 1
      this.cardIndexForm.items = []
    },
    // 获取数据
    async getData() {
      const res = await this.$api.prod.bom.index(this.postForm)
      if (res.code == 200) {
        this.hasMore = res.data.list.length ? true : false
        const list = []
        res.data.list.forEach((item) => {
          list.push({
            row: item,
            head: {
              title: item.purchase_code,
              price: item.all_money
            },
            body: [
              {
                name: '商品编号',
                value: item.goods_code
              },
              {
                name: '商品名称',
                value: item.goods_name
              },
              {
                name: '数量',
                value: item.number
              }
            ]
          })
        })

        this.cardIndexForm.items = [...this.cardIndexForm.items, ...list]
        this.noData = !this.cardIndexForm.items.length
      }
    },
    // 添加
    add() {
      uni.navigateTo({ url: '/pages/home/bom_list/add?type=add' })
    },
    // 详情
    detail(row) {
      uni.navigateTo({
        url: `/pages/home/bom_list/add?type=detail&id=${row.id}`
      })
    },
    // 编辑
    edit(row) {
      if (row.examine_status == '1') {
      }
      uni.navigateTo({
        url: `/pages/home/bom_list/add?type=edit&id=${row.id}`
      })
    },
    // 删除
    async del(row) {
      const res = await this.$api.prod.bom.del({
        id: row.id
      })
      if (res.code == 200) {
        uni.showToast({
          title: res.msg,
          icon: 'none'
        })
        this.resetPaging()
        this.getData()
      }
    }
  }
}
</script>

<style>
page {
  padding: 92rpx 0 400rpx;
}
</style>
